package com.cardone.platform.common.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.common.dto.EntityTreeDto;

/**
 * 实体树
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class EntityTreeJdbcDao implements EntityTreeDao {
	@Override
	public int deleteByIds(final EntityTreeDto deleteEntityTree) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteEntityTree.getAttrs())) {
			model.putAll(deleteEntityTree.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteEntityTree.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final EntityTreeDto findEntityTree) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityTree, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityTree, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findEntityTree.getAttrs())) {
			model.putAll(findEntityTree.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final EntityTreeDto findEntityTree) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityTree);

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityTree, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findEntityTree.getAttrs())) {
			model.putAll(findEntityTree.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final EntityTreeDto findListEntityTree) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListEntityTree).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListEntityTree.getAttrs())) {
			model.putAll(findListEntityTree.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final EntityTreeDto insertEntityTree) {
		if (StringUtils.isBlank(insertEntityTree.getId())) {
			insertEntityTree.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertEntityTree));

		if (MapUtils.isNotEmpty(insertEntityTree.getAttrs())) {
			model.putAll(insertEntityTree.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final EntityTreeDto paginationEntityTree) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationEntityTree).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationEntityTree.getAttrs())) {
			model.putAll(paginationEntityTree.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationEntityTree), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final EntityTreeDto readEntityTree) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readEntityTree, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readEntityTree.getAttrs())) {
			model.putAll(readEntityTree.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readEntityTree);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readEntityTree, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final EntityTreeDto saveEntityTree) {
		final P oldEntityTree = this.findByCodeOrId(mappedClass, saveEntityTree);

		if (oldEntityTree == null) {
			this.insertByCode(saveEntityTree);

			return this.findByCodeOrId(mappedClass, saveEntityTree);
		}

		final int updateCount = this.updateByCode(saveEntityTree, oldEntityTree);

		if (updateCount < 1) {
			return oldEntityTree;
		}

		return this.findByCodeOrId(mappedClass, saveEntityTree);
	}

	@Override
	public int updateByCode(final EntityTreeDto updateEntityTree) {
		final EntityTreeDto oldEntityTree = this.findByCodeOrId(EntityTreeDto.class, updateEntityTree);

		return this.updateByCode(updateEntityTree, oldEntityTree);
	}

	private <P> int updateByCode(final EntityTreeDto updateEntityTree, final P oldEntityTree) {
		if (oldEntityTree == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateEntityTree);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateEntityTree, oldEntityTree, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldEntityTree, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldEntityTree, updateEntityTree, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateEntityTree, updateProperties), new ModelArgs(updateEntityTree, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateEntityTree.getAttrs())) {
			model.putAll(updateEntityTree.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}